﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SpeakupBackend.Themes;
using System.IO;

namespace SpeakupTests.SpeakupBackend.Themes.Club
{
    [TestClass]
    public class ClubThemeCollectionTests
    {
        ClubThemeCollection ThemeCollection;

        [TestInitialize]
        public void TestInitialize()
        {
            ThemeCollection = new ClubThemeCollection();

            foreach (ClubLevelGroup lg in ClubLevelGroup.ClubFactory.GetLevelGroups())
            {
                ThemeCollection.Add(new ClubTheme("name", lg));
            }

            Assert.AreEqual(ThemeCollection.Count, ClubLevelGroup.ClubFactory.GetLevelGroups().Count);            
        }

        [TestMethod]
        public void Create()
        {
            ClubThemeCollection themes = new ClubThemeCollection();

            foreach (ClubLevelGroup lg in ClubLevelGroup.ClubFactory.GetLevelGroups())
            {
                themes.Add(new ClubTheme("name", lg));
            }

            Assert.AreEqual(themes.Count, ClubLevelGroup.ClubFactory.GetLevelGroups().Count);
        }

        [TestMethod]
        public void SaveLoad()
        {
            String tmpFileName = Path.GetTempFileName();
                        
            ThemeCollection.Save(tmpFileName);
            ClubThemeCollection deserialized = ClubThemeCollection.Load(tmpFileName);
            
            Assert.AreEqual(ThemeCollection.Count, deserialized.Count);

            foreach(ClubTheme thOrig in ThemeCollection)
            {
                foreach(ClubTheme thDeserialized in deserialized)
                {
                    Assert.IsTrue(deserialized.Contains(thOrig));
                    Assert.IsTrue(ThemeCollection.Contains(thDeserialized));

                }
            }
        }

        [TestMethod]
        public void SaveReload()
        {
            ClubThemeCollection copiedCollection = new ClubThemeCollection();

            foreach (ClubTheme theme in ThemeCollection)
            {
                copiedCollection.Add(theme);
            }

            
            String tmpFileName = Path.GetTempFileName();
            copiedCollection.Save(tmpFileName);

            copiedCollection.Clear();

            copiedCollection.ReloadFrom(tmpFileName);


            Assert.AreEqual(ThemeCollection.Count, copiedCollection.Count);
            foreach (ClubTheme thOrig in ThemeCollection)
            {
                foreach (ClubTheme thDeserialized in copiedCollection)
                {
                    Assert.IsTrue(copiedCollection.Contains(thOrig));
                    Assert.IsTrue(ThemeCollection.Contains(thDeserialized));

                }
            }
        }
    }
}
